package GUI;

import Control.User;
import RatRoid.Main;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import res.flags.CountryCode;

public class MainGui extends javax.swing.JFrame {


    public MainGui() {
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        start.setEnabled(false);
        Tuser.jTable1.getColumnModel().getColumn(0).setCellRenderer(new MyRenderer());
        Tuser.jTable1.setRowHeight(30);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

        Tuser.jTable1.addMouseListener(new MouseAdapter() { 
            @Override
            public void mousePressed(MouseEvent e){check(e);}
            @Override
            public void mouseReleased(MouseEvent e){check(e);}
            public void check(MouseEvent e) {
                int r = Tuser.jTable1.rowAtPoint(e.getPoint());
                if (r >= 0 && r < Tuser.jTable1.getRowCount()) {
                    Tuser.jTable1.setRowSelectionInterval(r, r);
                } else {
                    Tuser.jTable1.clearSelection();
                }

                int rowindex = Tuser.jTable1.getSelectedRow();
                if (rowindex < 0) {
                    return;
                }
                if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
                    PopupMenu popup = new PopupMenu();
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        this.setVisible(true);
        //tabbedPane.setUI(new ModernTabbedPaneUI());
    }

    public void addUser(User moi) {

        if (moi.getimg() == null) {
            moi.setimg("unknown");
        }
        if (moi.getimei() == null) {
            moi.setimei("unknown");
        }
        if (moi.gettel() == null) {
            moi.settel("Unknown");
        }
        if (moi.getsimserial() == null) {
            moi.setsimserial("Unknown");
        }
        if (moi.getoperator() == null) {
            moi.setoperator("Unknown");
        }
        if (moi.getdevice() == null) {
            moi.setdevice("unknown");
        }
        
        Tuser.mymodel.addUser(moi); 
    }

    public void removeUser(String imei) {

        Tuser.mymodel.removeUser(imei);
        getContentPane().repaint();

    }
    
    public void modifyUser(String imei,String value) {

        Tuser.mymodel.modifyUser(imei, value);
        getContentPane().repaint();

    }
    
    public void addStats(Stats moi) {

        if (moi.getimg() == null) {
            moi.setimg("unknown");
        }
        if (moi.getimei() == null) {
            moi.setimei("unknown");
        }
        
        if (moi.getdevice() == null) {
            moi.setdevice("unknown");
        }
        
        Tstats.mymodel.addStat(moi); 
    }
    
    
public class MyRenderer extends DefaultTableCellRenderer
	{

    @Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			String str = (String) value;
                        CountryCode cc = CountryCode.getByCode(str.toUpperCase());
                        String name=str;

			ImageIcon icon;
			File f = new File("./res/flags/"+str.toUpperCase()+".png");
			if (f.exists())
			{
                            
                            name=cc.getName();
                            icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/flags/"+str.toUpperCase()+".png")));
			} else{
			icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/flags/unknown.png")));}
                        setText(name);
			setIcon(icon);
			return this;
		}
	}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        conntext = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        porttext = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        statusLabel = new javax.swing.JLabel();
        MenuBar = new javax.swing.JMenuBar();
        ServerMenu = new javax.swing.JMenu();
        start = new javax.swing.JMenuItem();
        stop = new javax.swing.JMenuItem();
        AboutMenu = new javax.swing.JMenu();
        about = new javax.swing.JMenuItem();
        legal = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RatRoid"+"["+RatRoid.Main.nbcliens+"]");
        setMinimumSize(new java.awt.Dimension(5, 5));
        setPreferredSize(new java.awt.Dimension(754, 444));
        getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER);
        ImageIcon icon0= new javax.swing.ImageIcon(getClass().getResource("/res/androo.png"));
        Image img0 = icon0.getImage();
        Image newimg0 = img0.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
        ImageIcon iconres0 = new ImageIcon(newimg0);
        tabbedPane.addTab("Users",iconres0,Tuser,"Connected Devices (Control center)");
        ImageIcon icon1= new javax.swing.ImageIcon(getClass().getResource("/res/stats.png"));
        Image img1 = icon1.getImage();
        Image newimg1 = img1.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
        ImageIcon iconres1 = new ImageIcon(newimg1);
        tabbedPane.addTab("Statistics",iconres1, Tstats,"Server and Users Statistics");
        ImageIcon icon2= new javax.swing.ImageIcon(getClass().getResource("/res/computer.png"));
        Image img2 = icon2.getImage();
        Image newimg2 = img2.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
        ImageIcon iconres2 = new ImageIcon(newimg2);
        tabbedPane.addTab("Information",iconres2, panelInfo,"Local Computer/Server Informations");

        jPanel1.setDoubleBuffered(false);
        jPanel1.setPreferredSize(new java.awt.Dimension(14, 19));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        conntext.setText(" Connected Clients : 0");
        jPanel1.add(conntext);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setMaximumSize(new java.awt.Dimension(1, 1));
        jSeparator2.setMinimumSize(new java.awt.Dimension(1, 1));
        jSeparator2.setPreferredSize(new java.awt.Dimension(1, 1));
        jPanel1.add(jSeparator2);

        porttext.setText("Listening on port : ");
        jPanel1.add(porttext);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setMaximumSize(new java.awt.Dimension(1, 1));
        jSeparator1.setMinimumSize(new java.awt.Dimension(1, 1));
        jSeparator1.setPreferredSize(new java.awt.Dimension(1, 1));
        jPanel1.add(jSeparator1);

        statusLabel.setText("Status : Initialized");
        jPanel1.add(statusLabel);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        MenuBar.setBackground(new java.awt.Color(0, 0, 0));

        ServerMenu.setText("Server ");

        start.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/connect.png"))); // NOI18N
        start.setText("Start Server");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });
        ServerMenu.add(start);

        stop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/disconnect.png"))); // NOI18N
        stop.setText("Stop Server");
        stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopActionPerformed(evt);
            }
        });
        ServerMenu.add(stop);

        MenuBar.add(ServerMenu);

        AboutMenu.setText("About");

        about.setText("AboutUs");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });
        AboutMenu.add(about);

        legal.setText("Limitation de responsabilité ");
        legal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                legalActionPerformed(evt);
            }
        });
        AboutMenu.add(legal);

        MenuBar.add(AboutMenu);

        setJMenuBar(MenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
        new AboutUs();
    }//GEN-LAST:event_aboutActionPerformed

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        Main.start();
        start.setEnabled(false);
        stop.setEnabled(true);
    }//GEN-LAST:event_startActionPerformed

    private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
        Main.stop();
        start.setEnabled(true);
        stop.setEnabled(false);
    }//GEN-LAST:event_stopActionPerformed

    private void legalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_legalActionPerformed
        new Legal();
    }//GEN-LAST:event_legalActionPerformed

    public JLabel getStatus(){
        return statusLabel;
    }
    StatsTab Tstats = new StatsTab();
    static UserTab Tuser = new UserTab();
    
    public InfoPanel getInfoPanel(){return panelInfo;}
    private InfoPanel panelInfo = new InfoPanel();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu AboutMenu;
    private javax.swing.JMenuBar MenuBar;
    private javax.swing.JMenu ServerMenu;
    private javax.swing.JMenuItem about;
    public static javax.swing.JLabel conntext;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JMenuItem legal;
    public static javax.swing.JLabel porttext;
    private javax.swing.JMenuItem start;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JMenuItem stop;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
}